<template>
  <div class="app-container">
    <!-- 添加或修改路线管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8" :xs="24">
            <el-form-item label="小区ID" prop="communityId">
              <el-select v-model="form.communityId" style="width: 100%;" placeholder="请选择所属小区">
                <el-option
                  v-for="dict in communityOptions"
                  :key="dict.id"
                  :label="dict.communityName"
                  :value="dict.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="路线名称" prop="pathName">
              <el-input v-model="form.pathName" placeholder="请输入路线名称" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="巡逻人员" prop="crulserId">
              <el-input v-model="form.crulserId" placeholder="请输入巡逻人员" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="巡逻开始时间" prop="crulserBegin">
              <el-input v-model="form.crulserBegin" placeholder="请输入巡逻开始时间" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="巡逻结束时间" prop="crulserEnd">
              <el-input v-model="form.crulserEnd" placeholder="请输入巡逻结束时间" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="巡逻类型">
              <el-radio-group v-model="form.crulserType" :disabled="disabled">
                <el-radio
                  v-for="item in dict.crulser_type"
                  :key="item.dictValue"
                  :label="item.dictValue"
                >{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPath , addPath, updatePath } from "@/api/waitress/path";
import { listCommunity} from "@/api/property/community";
export default {
  name: "PathFrom",
  dicts: [   'crulser_type',   ],
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否禁用
      disabled: false,
      communityOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        communityId: [
          { required: true, message: "小区ID不能为空", trigger: "blur" }
        ],
        pathName: [
          { required: true, message: "路线名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    listCommunity().then(res=>{
      this.communityOptions = res.rows;
    })
  },
  methods: {
    show(option){
      this.reset();
      this.disabled = option.type == 'VIEW'
      if(option.type=='ADD'){
          this.open = true;
          this.title = "新增路线管理";
      }else if(option.type == 'UPDATE' || option.type == 'VIEW'){
          getPath(option.id).then(response => {
              this.form = response.data;

              if(option.type == 'VIEW'){
                  this.title = "查看路线管理";
              }else{
                  this.title = "修改路线管理";
              }
              this.open = true;
          });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("cancel")
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        communityId: null,
        pathName: null,
        crulserId: null,
        crulserBegin: null,
        crulserEnd: null,
        crulserType: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        ancestors: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePath(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.cancel()
            });
          } else {
            addPath(this.form).then(response => {
              this.msgSuccess("新增成功");
                this.cancel()
            });
          }
        }
      });
    }
  }
};
</script>
